package tool

import (
	"fmt"
	"math/rand"
	"time"
)

func PrintSlice(arr []int) {

	for i := 0; i < len(arr); i++ {
		fmt.Print(arr[i], " ")
	}
	fmt.Println()
}

func Swap(arr []int, i, minvalue int) {
	var tmp = arr[minvalue]
	arr[minvalue] = arr[i]
	arr[i] = tmp
}

func SelectSort(arr []int) { //选择排序

	if arr == nil || len(arr) < 2 {
		return
	}

	//获取数组的长度
	N := len(arr)

	for i := 0; i < N; i++ {
		minvalue := i

		for j := i + 1; j < N; j++ {
			if arr[minvalue] > arr[j] {
				minvalue = j
			}
		}

		Swap(arr, i, minvalue)

	}

}
func LenRandomValueRandom(maxlen, maxvalue int) (bool, []int) {
	if maxlen < 2 {
		fmt.Println("数组长度不符合要求")
		return false, nil
	}
	rand.Seed(time.Now().Unix())
	var arr = make([]int, rand.Intn(maxlen-2)+2, maxlen)

	for i := 0; i < len(arr); i++ {

		arr[i] = rand.Intn(maxvalue + 1)
	}
	return true, arr

}

func CopySilce(arr []int) []int {

	var arr1 = make([]int, len(arr), cap(arr))
	for i := 0; i < len(arr); i++ {
		arr1[i] = arr[i]

	}
	return arr1

}

func EqualArr(arr1, arr2 []int) bool {
	if len(arr1) != len(arr2) {
		fmt.Println("数组长度不相等")
		return false
	}
	for i := 0; i < len(arr1); i++ {
		if arr1[i] != arr2[i] {
			return false
		}
	}
	return true
}
func IsSort(arr []int) bool {

	if len(arr) < 2 {

		return true
	}

	max := arr[0]

	for i := 1; i < len(arr); i++ {
		if max > arr[i] {
			return false
		}
		max = arr[i]
	}
	return true
}
func InsertSort2(arr []int) {

	if arr == nil || len(arr) < 2 {
		return
	}
	N := len(arr)
	for end := 1; end < N; end++ {

		for pre := end; pre-1 >= 0 && arr[pre-1] > arr[pre]; pre-- { //为了防止类似于零的元素一直往前换导致下标变成-1越界

			Swap(arr, pre-1, pre)

		}

	}

}
